
#include <BFont>
#include <BStyle>
#include <BReadWrite>
#include "member_BIcon.h"

using namespace BWE;

BImage* BWE::ConvertRGBA(BImage* image, int state)
{
	if (!image)
		return 0;
	BImage* newimage = new BImage(image->size(), image->format());
	BColor* data0 = (BColor*)newimage->pixels()->data();
	BColor* data1 = (BColor*)image->pixels()->data();
	int count = image->size().width() * image->size().height();
	for (int i = 0; i < count; i++)
	{
		data0[i] = data1[i];
		BColor& color = data0[i];
		switch (state)
		{
		case State_Pressed:
		{
			color %= 83;
			break;
		}
		case State_Hovered:
		{
			color %= 116;
			break;
		}
		case State_Selected:
		{
			color %= 124;
			break;
		}
		case State_Disable:
		{
			BByte gray = color.gray();
			color.r() = gray;
			color.g() = gray;
			color.b() = gray;
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* BWE::ConvertRGB(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height() * 3;
	for (int i = 0; i < count; i += 3)
	{
		BByte& r = data0[i];
		BByte& g = data0[i + 1];
		BByte& b = data0[i + 2];
		r = data1[i];
		g = data1[i + 1];
		b = data1[i + 2];
		switch (state)
		{
		case State_Pressed:
		{
			r = (BByte)(r*0.83f);
			g = (BByte)(g*0.83f);
			b = (BByte)(b*0.83f);
			break;
		}
		case State_Hovered:
		{
			int cr = (int)(r * 1.16f);
			int cg = (int)(g * 1.16f);
			int cb = (int)(b * 1.16f);
			r = cr > 255 ? 255 : cr;
			g = cg > 255 ? 255 : cg;
			b = cb > 255 ? 255 : cb;
			break;
		}
		case State_Selected:
		{
			int cr = (int)(r * 1.24f);
			int cg = (int)(g * 1.24f);
			int cb = (int)(b * 1.24f);
			r = cr > 255 ? 255 : cr;
			g = cg > 255 ? 255 : cg;
			b = cb > 255 ? 255 : cb;
			break;
		}
		case State_Disable:
		{
			BByte gray = BByte((r + g + b) * 0.333333f);
			r = gray;
			g = gray;
			b = gray;
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* BWE::ConvertGray(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height();
	for (int i = 0; i < count; i++)
	{
		data0[i] = data1[i];
		switch (state)
		{
		case State_Pressed:
		{
			data0[i] = (BByte)(data0[i] * 1.24f);
			break;
		}
		case State_Hovered:
		{
			int gray = (BByte)(data0[i] * 1.64f);
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Selected:
		{
			int gray = (BByte)(data0[i] * 1.36f);
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Disable:
		{
			data0[i] = (BByte)(data0[i] * 0.83f);
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* BWE::ConvertAlpha(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height();
	for (int i = 0; i < count; i++)
	{
		switch (state)
		{
		case State_Pressed:
		{
			int gray = int(data1[i] * 1.24f);
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Hovered:
		{
			int gray = int(data1[i] * 1.1f);
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Selected:
		{
			int gray = int(data1[i] * 1.36f);
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Disable:
		{
			data0[i] = BByte(data1[i] * 0.83f);
			break;
		}
		default:
			data0[i] = data1[i];
			break;
		}
	}
	return newimage;
}
BImage* BWE::ConvertGrayAlpha(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height() * 2;
	for (int i = 0; i < count; i += 2)
	{
		data0[i] = data1[i];
		data0[i + 1] = data1[i + 1];
		if (state == State_Pressed)
		{
			data0[i] = (BByte)(data0[i] * 0.24f);
		}
		else if (state == State_Hovered)
		{
			int gray = (BByte)(data0[i] * 1.64f);
			data0[i] = gray > 255 ? 255 : gray;
		}
		else if (state == State_Disable)
		{

		}
	}
	return newimage;
}

member_BIcon::member_BIcon(BIcon* icon)
{
	boss = icon;
}
member_BIcon::~member_BIcon()
{

}

void member_BIcon::makeImage(State state)
{
	BImage* image = images[State_None];
	if (!image)
		return;
	if (state == State_Normal && images[State_Normal] == 0)
	{
		images[State_Normal] = image;
		return;
	}
	if (state == State_Actived && images[State_Actived] == 0)
	{
		images[State_Actived] = image;
		return;
	}
	if (state == State_Checked && images[State_Checked] == 0)
	{
		images[State_Checked] = image;
		return;
	}
	if (state & State_Disable && images[State_Disable] == 0)
	{
		images[State_Disable] = ConvertImage(image, State_Disable);
	}
	int flag = state;
	if (flag & State_Normal)
	{
		image = images[State_Normal];
		flag &= ~State_Normal;
	}
	else if (flag & State_Actived)
	{
		image = images[State_Actived];
		flag &= ~State_Actived;
	}
	else if (flag & State_Checked)
	{
		image = images[State_Checked];
		flag &= ~State_Checked;
	}
	else if (flag & State_Disable)
	{
		image = images[State_Disable];
		flag &= ~State_Disable;
	}
	if (!image)
	{
		image = images[State_None];
	}
	if (flag)
	{
		images[state] = ConvertImage(image, flag);
	}
}
